﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Xarmarin.GoldFinger.Common.Utils
{
    internal class KnifeHelper
    {
        #region 附件操作类
        /// <summary>
        /// 获取文件大小 bit=>kb mb G T
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string GetFileSize(long size)
        {
            const double num = 1024.00; //byte
            if (size < num)
                return size + "B";
            if (size < Math.Pow(num, 2))
                return (size / num).ToString("f2") + "kb"; //kb
            if (size < Math.Pow(num, 3))
                return (size / Math.Pow(num, 2)).ToString("f2") + "mb"; //M
            if (size < Math.Pow(num, 4))
                return (size / Math.Pow(num, 3)).ToString("f2") + "G"; //G

            return (size / Math.Pow(num, 4)).ToString("f2") + "T"; //T
        }
        #endregion

        #region AES加密
        /// <summary>
        ///  AES 加密
        /// </summary>
        /// <param name="str">明文（待加密）</param>
        /// <param name="key">密文</param>
        /// <returns></returns>
        public static string AesEncrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);

            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = rm.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        /// <summary>
        ///  AES 解密
        /// </summary>
        /// <param name="str">明文（待解密）</param>
        /// <param name="key">密文</param>
        /// <returns></returns>
        public static string AesDecrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }
        #endregion
        
        #region 自定义参数
        /// <summary>
        /// 随机数 【yyyyMMddHHmmss+5位随机数】
        /// </summary>
        public static string RandomCode
        {
            get
            {
                return DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(10000, 99999);
            }
        }
        /// <summary>
        /// AES秘钥-32位
        /// </summary>
        public static string AESKEY
        {
            get
            {
                return "12345678900987654321246897531101";
            }
        }
        #endregion
        
        #region Convert转换

        /// <summary>
        /// 相对路径转绝对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ConvertToAbsolutePath(string path)
        {
            if (path.StartsWith(@"/"))
            {
                path = path.Substring(1);
            }
            return AppDomain.CurrentDomain.BaseDirectory + path.Replace(@"/", @"\");
        }

        #endregion

        #region 静态变量

        #endregion
    }
}
